﻿using StartsEAS_API.DAL.DALTools;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.AlterStuD
{
    public class AlterStuDAL
    {
        private readonly StarsEASContext _context;
        public AlterStuDAL(StarsEASContext context) {
            _context = context;
        }
        /// <summary>
        /// 获取所有学生信息修改申请表
        /// </summary>
        /// <returns></returns>
        public ResultUtils GetAllAlterStu() {
            List<AlterStu> alterStuList = _context.AlterStus.ToList();
            return new ResultUtils() { code = 200, message = alterStuList };
        }

        /// <summary>
        /// 查询该学生的所有申请记录
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultUtils GetAllTheRecordsByToken(string token) {
            Model.Models.Student student = token.GetStudentByToken(_context);
            if (student == null) return new ResultUtils() { code = 201, message = "Token错误" };
            List<AlterStu> alterStusList = _context.AlterStus
                .OrderByDescending(a => a.AlterToApplyForTime)
                .Where(a=>a.StuId==student.StuId).ToList();
            return new ResultUtils() { code = 200, message = alterStusList };
        }

        /// <summary>
        /// 根据学生编号查询现有学生信息修改申请表信息和现在的学生信息
        /// </summary>
        /// <param name="stuId">学号</param>
        /// <returns></returns>
        public ResultUtils GetAlterStuByStuId(string stuId) {
            Model.Models.Student oldStu = _context.Students.Where(x => x.StuId == stuId).FirstOrDefault();
            AlterStu newStu = _context.AlterStus.OrderByDescending(a => a.AlterToApplyForTime).Where(x => x.StuId == stuId && x.AlterStuStatus!=(int)TheExaminationAndApproval.ApprovalToComplete).FirstOrDefault();
            return new ResultUtils() { code = 200, message = new {
                oldInfo = oldStu,
                newInfo = newStu
            } };
        }
        /// <summary>
        /// 获取所有目前教务处待审批的学生信息修改申请表
        /// </summary>
        /// <returns></returns>
        public ResultUtils GetAllAlterStuStatusIsNotOk() {
            List<AlterStu> alterStusList = _context.AlterStus
                .OrderByDescending(a => a.AlterToApplyForTime)
                .Where(x => x.AlterStuStatus.Equals(TheExaminationAndApproval.TheTeacherAgreedTo)
                )
                .ToList();
            return new ResultUtils() { code = 200, message = alterStusList };
        }
        
        /// <summary>
        /// 根据所属辅导员进行查询辅导员需要审批的学生信息修改申请表
        /// </summary>
        /// <param name="WorkerId">辅导员编号</param>
        /// <returns></returns>
        public ResultUtils GetAlterStuByCounselor(string token)
        {
            Worker worker = token.GetWorkerByToken(_context);
            if (worker == null) return new ResultUtils() { code = 201, message = "Token错误" };
            var data = _context.AlterStus.Where(x => x.WorkerId.Equals(worker.WorkerId) &&
            x.AlterStuStatus.Equals(TheExaminationAndApproval.StudentsToApplyFor)
            ).ToList();
            return new ResultUtils() { code = 200, message = data };
        }
        /// <summary>
        /// 根据所属辅导员进行查询该辅导员所有需要审批的学生信息修改申请表信息
        /// </summary>
        /// <param name="token">token</param>
        /// <returns></returns>
        public ResultUtils GetAllAlterStuByCounselor(string token)
        {
            Worker worker = token.GetWorkerByToken(_context);
            List<AlterStu> alterStusList = _context.AlterStus
                .OrderByDescending(a => a.AlterToApplyForTime)
                .Where(x => x.WorkerId.Equals(worker.WorkerId)).ToList();
            return new ResultUtils() { code = 200, message = alterStusList };
        }
        /// <summary>
        /// 添加学生信息修改申请表
        /// </summary>
        /// <param name="stu">新学生信息</param>
        /// <returns></returns>
        public ResultUtils InsertAlterStu(AlterStu stu) {
            _context.AlterStus.Add(stu);
            int result = _context.SaveChanges();
            return new ResultUtils() { code = 200, message = result };
        }
        /// <summary>
        /// 教师批阅学生信息修改申请表
        /// </summary>
        /// <param name="token">token</param>
        /// <param name="status">审批状态 0 同意 ;1 拒绝</param>
        /// <returns></returns>
        public ResultUtils AlterStuCounselorApproval(string token, int status, string stuId) {
            Worker worker = token.GetWorkerByToken(_context);
            if (worker == null) return new ResultUtils() { code = 201, message = "token无效" };
            AlterStu student = _context.AlterStus
                .OrderByDescending(a => a.AlterToApplyForTime)
                .Where(x => x.StuId == stuId).FirstOrDefault();
            if (status == 0) {
                student.AlterStuStatus = (int)TheExaminationAndApproval.TheTeacherAgreedTo;//教师同意
            }
            else {
                student.AlterStuStatus = (int)TheExaminationAndApproval.TheTeacherRefusedTo; ;//教师拒绝
            }
            int result = _context.SaveChanges();
            return new ResultUtils() { code = 200, message = result };
        }
        /// <summary>
        /// 教务处批阅学生信息修改申请表
        /// </summary>
        /// <param name="token">token</param>
        /// <param name="status">审批状态 0 同意 ;1 拒绝</param>
        /// <returns></returns>
        public ResultUtils AlterStuDeptApproval(string token, int status, string stuId) {
            Worker worker = token.GetWorkerByToken(_context);
            if (worker == null) return new ResultUtils() { code = 201, message = "token无效" };
            if (worker.PositionId != 6) return new ResultUtils() { code = 202, message = "权限不足" };
            AlterStu studentAlter = _context.AlterStus
                .OrderByDescending(a => a.AlterToApplyForTime)
                .Where(x => x.StuId == stuId).FirstOrDefault();
            if (status == 0)
            {
                studentAlter.AlterStuStatus = (int)TheExaminationAndApproval.ApprovalToComplete;//教务处同意
                Model.Models.Student student = _context.Students.Where(x => x.StuId == stuId).FirstOrDefault();
                student.StuEmail = studentAlter.AlterStuemail;
                student.StuName = studentAlter.AlterStuName;
                student.StuAge = studentAlter.AlterStuAge;
                student.StuSex = studentAlter.AlterStuSex;
                student.StuCarid = studentAlter.AlterStuCarid;
                student.ClassId = studentAlter.AlterClassId;
                student.StubackTime = studentAlter.AlterStubackTime;
                student.StuPhone = studentAlter.AlterStuPhone;
                student.PoliticalLandscape = studentAlter.AlterPoliticalLandscape;
                student.NativePlace = studentAlter.AlterNativePlace;
                student.National = studentAlter.AlterNational;
            }
            else
            {
                studentAlter.AlterStuStatus = (int)TheExaminationAndApproval.TheDeanSOfficeRefused;//教务处拒绝
            }
            int result = _context.SaveChanges();
            return new ResultUtils() { code = 200, message = result };
        }
        /// <summary>
        /// 根据状态进行自主查询学生信息修改申请表
        /// </summary>
        /// <param name="status">查询的状态</param>
        /// <returns></returns>
        public ResultUtils AlterStuByStatus(int status) {
            return new ResultUtils() { code = 200, message = _context.AlterStus.Where(x => x.AlterStuStatus!=status).ToList() };
        }
    }
}
